<script>
	import {
		getWxOpenId,
		getAccessToken,
		getUserInfo,
		registerUser
	} from "@/plugins/api/userApi.js"

	export default {
		// 添加全局变量，控制用户隐私协议
		globalData: {
			showPrivacy: false
		},
		onLaunch: function(options) {
			console.log('App Launch')
			let openId = this.$store.state.user.openId
			if (openId) {
				// 直接获取用户数据
				getUserInfo({
					openId: openId
				}).then(async ures => {
					if (ures.data.status == 1) {
						// 用户信息不存在
						return false
					} else {
						await this.$store.dispatch('user/setUserInfoAction', ures.data.data)
						return true
					}
				}).catch(err => {
					console.log(err)
				})
			} else {
				// 初始化用户数据
				return new Promise((resolve, reject) => {
					try {
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								uni.showLoading({
									title: '登录中...'
								})
								// 获取微信openId
								try {
									getWxOpenId({
										code: loginRes.code
									}).then(async res => {
										this.$store.dispatch('user/setOpenIdAction',
											res.data.data.openId)
										this.$store.dispatch(
											'user/setSessionKeyAction', res.data
											.data.sessionKey)
										// 获取用户信息，若用户存在则返回用户信息，不存在则进行注册登记
										getUserInfo({
											openId: res.data.data.openId
										}).then(async ures => {
											if (ures.data.status == 1) {
												uni.hideLoading()
												// 用户不存在，需要注册
												return false
											} else {
												console.log('加载用户信息')
												// 用户存在
												resolve(await this.$store
													.dispatch(
														'user/setUserInfoAction',
														ures.data.data)
												)
												uni.hideLoading()
											}
										}).catch(e => {
											console.log(e)
										})
									}).catch(err => {
										console.log(err)
									})
								} catch (e) {
									console.log(e, 'truchat')
								}
							},
							fail: function(err) {
								reject(err)
								uni.hideLoading()
								uni.showToast({
									icon: 'none',
									title: '登录失败，请点击登录'
								})
							}
						})
					} catch (e) {
						uni.showToast({
							icon: 'none',
							title: e
						})
					}
				}).catch((e) => {})
			}
			// 用户隐私协议获取
			if (uni.getPrivacySetting) {
				uni.getPrivacySetting({
					success: res => {
						console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
						if (res.needAuthorization) {
							getApp().globalData.showPrivacy = true;
						} else {
							getApp().globalData.showPrivacy = false;
						}
					},
					fail: () => {},
					complete: () => {},
				})
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>
<style lang="scss">
	/*每个页面公共css */
	@import './static/styles/common.scss';
	@import url('static/iconfont/iconfont-weapp-icon.css');

	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
		font-family: PingFang SC;
	}

	// 禁止底部栏图标长按弹出菜单
	.uni-tabbar__icon>img {
		pointer-events: none;
	}
</style>